package com.nuanshui.heatedloan.web.credit;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.credit.facade.MerInfoService;
import com.nuanshui.heatedloan.credit.form.MerInfoForm;
import com.nuanshui.heatedloan.credit.vo.MerInfoVO;
import com.nuanshui.heatedloan.pageable.Pager;
import com.nuanshui.heatedloan.web.ApiController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author zss
 * @date 2017/8/1 16:29
 * @description
 * @Since
 */

@RequestMapping("/cms/manage/merinfo")
@RestController
public class MerInfoController extends ApiController {

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout= GlobalParam.DUBBO_SERVER_TIME_OUT,version =GlobalParam.CREDIT_DUBBO_SERVER_VERSION )
    private MerInfoService merInfoService;

    /**
     * 获取商户信息
     * @param pager
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public String load(Pager<MerInfoVO> pager){
        pager=merInfoService.loadMerInfo(pager);
        return success(pager.asHashMap());
    }

    /***
     * 新增
     * @param merInfoForm
     */
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String saveMerInfo(MerInfoForm merInfoForm){
        boolean flag= merInfoService.saveMerInfo(merInfoForm)>0?true:false;
        if(flag){
            return success("新增成功");
        }
        return fail("新增失败");
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(String id,int defaultMer){
        MerInfoVO merInfoVO = merInfoService.loadDefaultMerInfo();
        if( merInfoVO!= null){
            return fail("原默认商户需要先设置为非默认状态");
        }
        boolean flag= merInfoService.updateDefaultMerInfo(id,defaultMer)>0?true:false;
        if(flag){
            return success("修改成功");
        }
        return fail("新增失败");
    }

}
